<template>
  <div class="dashboard-editor-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="">
        <el-radio-group v-model="queryParams.type" @change="typeChange">
          <el-radio-button label="1">日</el-radio-button>
          <el-radio-button label="2">月</el-radio-button>
          <el-radio-button label="3">年</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" prop="" v-show="queryParams.type == 1">
        <el-date-picker v-model="queryParams.time1" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="" v-show="queryParams.type == 2">
        <el-date-picker v-model="queryParams.time2" type="monthrange" range-separator="至" start-placeholder="开始月份"
          end-placeholder="结束月份" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="" v-show="queryParams.type == 3">
        <year-picker v-model="year" start-placeholder="开始年份" end-placeholder="结束年份" @picker-change="yearChange" />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <panel-group @handleSetLineChartData="handleSetLineChartData" ref="childComponent0" />
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          订单趋势
          <line-chart :chart-data="lineChartData1" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          售后趋势
          <line-chart :chart-data="lineChartData2" />
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          消费分析
          <line-chart :chart-data="lineChartData3" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          订单处理
          <pie-chart ref="childComponent1" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import BarChart from "./dashboard/BarChart";
import { getDashboard } from "@/api/dashboard";
import yearPicker from "@/components/YearRangePicker";
const lineChartData = {
  newVisitis: {
    expectedData: [
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
      { time: "2022-01-01", num: 0 },
    ],
    actualData: [121, 82, 91, 154, 162, 140, 145],
    type: 1,
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
    type: 2,
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
    type: 3,
  },
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    yearPicker,
  },
  data() {
    return {
      lineChartData1: lineChartData.newVisitis,
      lineChartData2: {},
      lineChartData3: {},
      queryParams: { type: 1, time1: [], time2: [] },
      showSearch: true,
      year: []
    };

  },
  async mounted() {
    try {
      // 在页面加载时调用接口获取数据
      await this.getData();
    } catch (error) {
      console.error("Error fetching data:", error);
    }
  },
  created() { },
  methods: {
    yearChange(e) {
      if (e && e.length) {
        this.queryParams.time2 = [e[0] + '-01-01', e[1] + '-12-31']
      } else {
        this.queryParams.time2 = []
      }

    },
    typeChange(e) {
      this.queryParams.time1 = [];
      this.queryParams.time2 = [];
    },
    getData() {
      getDashboard({
        ...this.queryParams,
        type: this.queryParams.type == 3 ? 2 : this.queryParams.type,
      }).then((response) => {
        this.lineChartData1 = response.data.data.a;
        this.lineChartData2 = response.data.data.b;
        this.lineChartData3 = response.data.data.c;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getData();
      this.$refs.childComponent0.getData({
        ...this.queryParams,
        type: this.queryParams.type == 3 ? 2 : this.queryParams.type,
      });
      this.$refs.childComponent1.getData({
        ...this.queryParams,
        type: this.queryParams.type == 3 ? 2 : this.queryParams.type,
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = { type: 1, time1: [], time2: [] };
      this.handleQuery();
    },
    handleSetLineChartData(type) { },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
